﻿using System;
using System.Collections.Generic;
using Rescue_of_the_Dwarvenzombies.SceneObjects.GameObjects.Characters.Enemies;
using Rescue_of_the_Dwarvenzombies.SceneObjects.GameObjects.Items;
using Rescue_of_the_Dwarvenzombies.SceneObjects.GameObjects.Items.ConsumableItems;

namespace Rescue_of_the_Dwarvenzombies.SceneObjects.SceneObjectsHandlers
{
    public class ItemHandler : IEnemyObserver
    {
        private List<ISceneObject> activeItems;
        private Random rand;

        /// <summary>
        /// Initializes a new instance of the ItemHandler class.
        /// </summary>
        public ItemHandler()
        {
            activeItems = new List<ISceneObject>();
            rand = new Random();
        }

        /// <summary>
        /// Add item from fallen enemy at said enemys position
        /// </summary>
        /// <param name="enemy"></param>
        public void AddItemFromEnemy(Enemy enemy)
        {
            activeItems.Add(new Potion(enemy.Position, this));
        }

        /// <summary>
        /// Random item spawn into the world
        /// </summary>
        public void AddItemWorldSpawn()
        {
            //Not yet implemented
        }

        /// <summary>
        /// Remove said item from the world
        /// </summary>
        /// <param name="item"></param>
        public void RemoveItem(Item item)
        {
            activeItems.Remove(item);
        }

        /// <summary>
        /// Check if the killed enemy drops item
        /// </summary>
        /// <param name="enemy"></param>
        public void IsDead(Enemy enemy)
        {
            CheckEnemyDropedItem(enemy);
        }

        /// <summary>
        /// Add item from enemy if the drop chance succeed
        /// </summary>
        /// <param name="enemy"></param>
        private void CheckEnemyDropedItem(Enemy enemy)
        {
            if (rand.Next(0, 100) <= enemy.ItemDropRate)
                AddItemFromEnemy(enemy);
        }

        /// <summary>
        /// Gets the items.
        /// </summary>
        /// <returns></returns>
        internal List<ISceneObject> GetItems()
        {
            return activeItems;
        }
    }
}